package com.sercomm.ott.mediaplayer;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ListView;

/**
 * Created by davis on 17-10-13.
 */

public class SimpleSwipeRefreshView extends SwipeRefreshLayout{
    private static final String TAG = SimpleSwipeRefreshView.class.getSimpleName();
    private final View mFooterView;
    private final int mScaledTouchSlop;
    private ListView mListView;
    private onLoadMoreListener mListener;
    private boolean isLoading;
    private RecyclerView mRecyclerView;
    private int mItemCount;
    private float mDownY, mUpY;

    public SimpleSwipeRefreshView(Context context, AttributeSet attr){
        super(context,attr);
        // 填充底部加载布局
        mFooterView = View.inflate(context,R.layout.simple_view_footler,null);
        // 表示控件移动的最小距离，手移动的距离大于这个距离才能拖动控件
        mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }
    @Override
    protected void onLayout(boolean changed,int left,int top,int right,int bottom){
        super.onLayout(changed,left,top,right,bottom);

        if(mListView == null || mRecyclerView == null){
            // 判断容器有多少个孩子
            if(getChildCount()>0){
                // 判断第一个孩子是不是ListView
                if(getChildAt(0) instanceof ListView){
                    // 创建ListView对象
                    mListView = (ListView) getChildAt(0);
                    // 设置ListView的滑动监听
                    setListViewOnScroll();
                }else if(getChildAt(0) instanceof RecyclerView){
                    mRecyclerView = (RecyclerView) getChildAt(0);
                    // 设置RecyclerView的滑动监听
                    setRecyclerViewOnScroll();
                }
            }
        }

    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev){
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                // 移动的起点
                mDownY =ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if(canLoadMore()){
                    loadData();
                }
                break;
            case MotionEvent.ACTION_UP:
                // 移动的终点
                mUpY = getY();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean canLoadMore(){
        // 1. 是上拉状态
        boolean condition1 = (mDownY - mUpY) >= mScaledTouchSlop;
        if (condition1) {
            Log.d(TAG, "up");
        }

        // 2. 当前页面可见的item是最后一个条目,一般最后一个条目位置需要大于第一页的数据长度
        boolean condition2 = false;
        if (mListView != null && mListView.getAdapter() != null) {

            if (mItemCount > 0) {
                if (mListView.getAdapter().getCount() < mItemCount) {
                    // 第一页未满，禁止下拉
                    condition2 = true;
                }else {
                    condition2 = false;
                    //condition2 = mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() - 1);
                }
            } else {
                // 未设置数据长度，则默认第一页数据不满时也可以上拉
                condition2 = mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() - 1);
            }

        }

        if (condition2) {
            Log.d(TAG, "condition 2 ");
        }
        // 3. 正在加载状态
        boolean condition3 = !isLoading;
        if (condition3) {
            Log.d(TAG, "not loading");
        }
        return condition1 && condition2 && condition3;
    }
    private void loadData(){
        if(mListener!=null){
            // 设置加载状态，让布局显示出来
            setLoading(true);
            mListener.onLoadMore();
        }
    }
    public void setLoading(boolean loading){
        // 修改当前的状态
        isLoading = loading;
        if(isLoading){
            mListView.addFooterView(mFooterView);
        }else {
            // 隐藏布局
            mListView.removeFooterView(mFooterView);
            // 重置滑动的坐标
            mDownY = 0;
            mUpY = 0;
        }
    }

    private void setListViewOnScroll(){
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {
                if(canLoadMore()){
                    loadData();
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });
    }
    private void setRecyclerViewOnScroll() {
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                // 移动过程中判断时候能下拉加载更多
                if (canLoadMore()) {
                    // 加载数据
                    loadData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }
    public void setItemCount(int itemCount){
        this.mItemCount = itemCount;
    }
    public interface onLoadMoreListener{
        void onLoadMore();
    }
    public void setOnLoadMoreListener(onLoadMoreListener listener){
        this.mListener = listener;
    }
}
